// 版权归陈超所有


#include "UI/WidgetController/CC_OverlayWidgetController.h"

#include "AbilitySystem/CC_AbilitySystemComponent.h"
#include "AbilitySystem/CC_AttributeSet.h"

void UCC_OverlayWidgetController::BroadcastInitialValues()
{
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = Cast<UCC_AttributeSet>(AttributeSet);
	}
	//广播属性变化
	OnHealthChanged.Broadcast(CC_AttributeSet->GetHealth());
	OnMaxHealthChanged.Broadcast(CC_AttributeSet->GetMaxHealth());
	OnManaChanged.Broadcast(CC_AttributeSet->GetMana());
	OnMaxManaChanged.Broadcast(CC_AttributeSet->GetMaxMana());
}

void UCC_OverlayWidgetController::BindCallbacksToDependencies()
{
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = Cast<UCC_AttributeSet>(AttributeSet);
	}
	/*监听属性变化*/
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetHealthAttribute()).AddLambda([this](const FOnAttributeChangeData& Data){OnHealthChanged.Broadcast(Data.NewValue);});
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetMaxHealthAttribute()).AddLambda([this](const FOnAttributeChangeData& Data){OnMaxHealthChanged.Broadcast(Data.NewValue);});
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetManaAttribute()).AddLambda([this](const FOnAttributeChangeData& Data){OnManaChanged.Broadcast(Data.NewValue);});
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetMaxManaAttribute()).AddLambda([this](const FOnAttributeChangeData& Data){OnMaxManaChanged.Broadcast(Data.NewValue);});
	
	/*EffectActorTag信息*/
	 if (CC_AbilitySystemComponent == nullptr)
	{
		CC_AbilitySystemComponent = Cast<UCC_AbilitySystemComponent>(AbilitySystemComponent);
	}
	
	CC_AbilitySystemComponent->OnEffectAssetTags.AddLambda(
		[this](const FGameplayTagContainer& AssetTags)
		{
			for(const FGameplayTag& Tag : AssetTags)
			{
				//对标签进行检测，如果不是信息标签，将无法进行广播
				FGameplayTag RequestGameplayTag = FGameplayTag::RequestGameplayTag(FName("Message"));
				if (Tag.MatchesTag(RequestGameplayTag))		//检测Tag是否是RequestGameplayTag的子标签
				{
					check(MessageWidgetDataTable);
					FUIWidgetRow* Row = GetDataTableRowByTag<FUIWidgetRow>(MessageWidgetDataTable, Tag);
					OnMessageWidgetRowSignature.Broadcast(*Row);
				}
			}
		}
	);
}
